package memecached;

import net.spy.memcached.MemcachedClient;
import net.spy.memcached.AddrUtil;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;

/**
 * Created by IntelliJ IDEA.
 * User: odpsoft
 * Date: 2009-4-21
 * Time: 15:17:22
 */
public class MemcachedTest {
    public static void main(String[] argv) {
        MemcachedClient c = null;
        try {
            c = new MemcachedClient(
                    AddrUtil.getAddresses("192.168.1.12:11211 192.168.1.14:11211")
                    );
            MyObject object = new MyObject();
            object.setName("Hello Memcache!");
            object.setOld("Good!");

            /* Store a value (async) for one hour*/
            //c.set("someKey", 3600, object);
            
            /* Retrieve a value (synchronously).*/
            Object myObject = c.get("someKey");

            System.out.println(((MyObject) myObject).getName());
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally {
            if (c != null)
                c.shutdown();
        }
    }

    private static class MyObject implements Serializable {
        private String name;
        private String old;

        public String getOld() {
            return old;
        }

        public String getName() {
            return name;
        }

        public void setOld(String old) {
            this.old = old;
        }

        protected void setName(String n) {
            name = n;
        }
    }
}
